//
//  HZHomeViewModel.swift
//  HansWeibo
//
//  Created by Hans on 16/8/13.
//  Copyright © 2016年 Hans. All rights reserved.
//

import UIKit

class HZHomeViewModel: NSObject {
    
    // 创建一个对象model
    var homeModel: HZHomeModel?
    // 微博等级图片
    var mbrankImage: UIImage?
    //微博认证图片
    var verifiedImage: UIImage?
    // 微博来源
    var sinaSourceStr: String?
    // 转发
    var repostsCountStr: String?
    // 评论
    var commentsCountStr: String?
    // 赞
    var attitudesCountStr: String?
    // 微博时间
    var sinaTimeStr: String?{
        return NSDate.getSinaDate(homeModel?.created_at ?? "").sinaDateStr
    }
    
    
    
    init(homeModel:HZHomeModel) {
        super.init()
        self.homeModel = homeModel
        mbrankImage = mbrankImageView(homeModel.user?.mbrank ?? 0)
        verifiedImage = verifiedImageView(homeModel.user?.verified ?? -1)
        sinaSourceStr = hzSinaSourceStr(homeModel.source ?? "")
        repostsCountStr = getSinaCountInt(homeModel.reposts_count ?? 0, title: "转发")
        commentsCountStr = getSinaCountInt(homeModel.comments_count ?? 0, title: "转发")
        attitudesCountStr = getSinaCountInt(homeModel.attitudes_count ?? 0, title: "转发")
    }
}




// MARK - 处理底部视图转发 评论 赞
extension HZHomeViewModel {
    
    // 处理底部视图转发 评论 赞
    func getSinaCountInt(count: Int, title: String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else {
            let c = CGFloat(count/1000)/10
            var cStr = "\(c)万"
            // 判断字符串 是否包含 .0
            if cStr.containsString(".0") {
                // 字符串替换
                cStr = cStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            return cStr
        }
    }
    
}


// MARK - 处理微博来源
extension HZHomeViewModel {
    //  处理微博来源
    func hzSinaSourceStr(source: String) -> String{
        // 判断字符串是否包含 特殊字符 ">
        if source.containsString("\">") {
            // 获取range
            let startRange = source.rangeOfString("\">")!
            let endRange = source.rangeOfString("</")!
            // 截取字符串
            let result = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
            return "来自\(result)"
        }
        // 如果为空
        return "来自天龙的汉子"
    }
}


// MARK - 处理cell中数据的逻辑
extension HZHomeViewModel {
    // 处理认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    //var verified: Int = 0
    func verifiedImageView(verified: Int) -> UIImage?{
        
        switch verified {
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    
    
    
    //    common_icon_membership_level1
    // 微博等级的Image
    func mbrankImageView(mbrank: Int) -> UIImage?{
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else{
            return UIImage(named: "common_icon_membership")
        }
        
    }
}